package br.ucb.cm.main.mainview.ui;

import java.beans.PropertyVetoException;

import javax.swing.JInternalFrame;
import java.awt.BorderLayout;

import br.ucb.cm.board.ui.BoardPane;
import br.ucb.cm.main.common.GeneralConstants.PlayerEnum;
import br.ucb.cm.main.mainview.IfcGameController;

import javax.swing.JPanel;
import javax.swing.BoxLayout;

public class GamePane extends JInternalFrame{
	private static final long serialVersionUID = 1L;
	private Counter playerICounter;
	private Counter playerIICounter;
	private IfcGameController controller;
	private BoardPane boardPane;

	public GamePane(IfcGameController gameController) {
		this.controller = gameController;
		setTitle("Novo Jogo");
		setClosable(true);
		setMaximizable(true);
		setResizable(true);
	}

	public void maximize() {
		try {
			setMaximum(true);
		} catch (PropertyVetoException e) {
			e.printStackTrace();
		}
	}
	
	public void showPane(){
		initComponents();
		setVisible(true);
		maximize();
	}

	private void initComponents() {
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		playerICounter = new Counter(PlayerEnum.PLAYER_ONE);
		getContentPane().add(playerICounter, BorderLayout.WEST);

		playerIICounter = new Counter(PlayerEnum.PLAYER_TWO);
		getContentPane().add(playerIICounter, BorderLayout.EAST);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		panel.add(boardPane);
	}
	
	public BoardPane getBoardPane() {
		return boardPane;
	}

	public void setBoardPane(BoardPane boardPane) {
		this.boardPane = boardPane;
	}

	public IfcGameController getController() {
		return controller;
	}

	public void setController(IfcGameController controller) {
		this.controller = controller;
	}

	public Counter getCounter(PlayerEnum player) {
		return player == PlayerEnum.PLAYER_ONE ? playerICounter : playerIICounter; 
	}

	public Counter getEnemyCounter(PlayerEnum player) {
		return player == PlayerEnum.PLAYER_ONE ? playerIICounter : playerICounter;
	}
	public void doVictory(PlayerEnum player) {
		getCounter(player).victory();
		getEnemyCounter(player).defeat();
	}

	public void draw() {
		getCounter(PlayerEnum.PLAYER_ONE).tie();
		getCounter(PlayerEnum.PLAYER_TWO).tie();
	}


}